package com.xxxx.mysql;

import com.xxxx.mysql.model.WormLink;
import com.xxxx.mysql.service.LinkService;
import com.xxxx.worm.GainMessage;
import com.xxxx.worm.HtmlParse;
import com.xxxx.worm.WormObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/link")
public class LinkController {

    @Autowired
    private LinkService linkService;


    @RequestMapping("/goWormLink")
    public String wormByUrk(String  url,String cssSelector){
//        WormLink wormLink1 = new WormLink();
//        linkService.insert(wormLink1);
        WormObj wormObj = GainMessage.gainMessageByNetwork(url);
        String html = wormObj.getHtml();
        if ("".equalsIgnoreCase(html)){
           return "未爬虫到东西";
       }
        try {
            List<WormLink> links = HtmlParse.getLinks(wormObj, cssSelector);
            for (WormLink wormLink:links){
                linkService.insert(wormLink);
            }
            return "成功："+links.size();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "特么的出错了";
    }

}
